package com.yinhe.module.wx;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.SecurityUtils;
import com.yinhe.system.service.ISysPostService;
import com.yinhe.system.service.ISysRoleService;
import com.yinhe.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


import com.yinhe.common.utils.ServletUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user/wx")
public class WXSecurityController extends BaseController  {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysPostService postService;

    /**
     * 根据用户名查询他的密保问题还有密码
     */
    @RequestMapping("/security/{username}")
    public AjaxResult security(@PathVariable String username){
      SysUser sysUser1    = userService.selectSecurityByUserName(username);
      System.out.println(sysUser1 +"user对象");
      //前台把密保问题填写进去
      return AjaxResult.success(sysUser1);
    }


    //根据问题查答案，跟表单提交的答案进行对比

    @PostMapping("/question/")
    public AjaxResult question(@RequestBody SysUser sysUser){


      int rows =   userService.selectAnswer(sysUser);
      int a =0;
      return AjaxResult.success(rows);
    }


    /**
     * 重置密码
     */

    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd/{newPassword}/{username}")
    public AjaxResult resetPwd(@PathVariable String newPassword,@PathVariable String username)
    {
        SysUser sysUser1    = userService.selectSecurityByUserName(username);

        SysUser user = new SysUser();

        user.setPassword(newPassword);
        user.setUserId(sysUser1.getUserId());
        user.setPassword(SecurityUtils.encryptPassword(newPassword));
        user.setUpdateBy(sysUser1.getUserName());

        return toAjax(userService.resetPwd(user));

    }


}

